package com.cxy.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cxy.dao.IAwardDao;
import com.cxy.entity.SysAward;

@Service("awardService")
public class AwardService {
	@Resource
	private IAwardDao awardDao;

	private final int fieldNum = 4;
	private final int showNum = 15;

	public String[][] list(int pageNum) {
		String[][] result = null;
		if (pageNum < 1) {
			return result;
		}
		List<SysAward> awards = new ArrayList<SysAward>();
		int beginNum = (pageNum - 1) * showNum;
		awards = awardDao.find(beginNum, showNum, "from SysAward", null);
		if (awards.size() > 0) {
			result = new String[awards.size()][fieldNum];
			for (int j = 0; j < awards.size(); j++) {
				buildResult(result, awards, j);
			}
		}
		return result;
	}

	public String[][] queryByLevel(String level) {
		String[][] result = null;
		if (level.length() < 0) {
			return result;
		}
		List<SysAward> awards = awardDao.list(
				"from SysAward where award_level=?", level);

		if (awards.size() > 0) {
			result = new String[awards.size()][fieldNum];
			for (int j = 0; j < awards.size(); j++) {
				buildResult(result, awards, j);
			}
		}
		return result;
	}

	public boolean add(SysAward award) {
		boolean flag = true;
		try {
			awardDao.add(award);
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}

	public void update(SysAward award) {
		awardDao.update(award);
	}

	public SysAward findByLevel(String level) {
		List<SysAward> list = awardDao.list(
				"from SysAward where award_level=?", level);
		return list.get(0);
	}

	// 将list中记录添加到二维数组中

	private void buildResult(String[][] result, List<SysAward> awards, int j) {
		SysAward award = awards.get(j);
		result[j][0] = String.valueOf(award.getId());
		result[j][1] = award.getAwardName();
		result[j][2] = String.valueOf(award.getAwardNum());
		result[j][3] = award.getAwardLevel();
	}

}
